package com.controller;

import com.po.Coach;
import com.po.Manager;
import com.po.User;
import com.service.CoachService;
import com.service.ManagerService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

/**
 * Created by 阳幸子 on 2018/4/20.
 */
@Controller
public class AccountController {
    @Autowired
    private UserService userService;

    @Autowired
    private ManagerService managerService;

    @Autowired
    private CoachService coachService;

    @RequestMapping(value = "/loginForm",method = RequestMethod.POST)
    public String login(String username, String password, String role, HttpSession session){
        if(role.equals("user")){
            User user =new User();
            user.setUsername(username);
            user.setPassword(password);
            System.out.println(user);
            User currentuser=userService.login(user);
            if(currentuser!=null){
                session.setAttribute("currentuser",currentuser);
            }
            return "/user/user";
        }else if(role.equals("manager")){
            Manager manager=new Manager();
            manager.setPassword(password);
            manager.setUsername(username);
            System.out.println(manager);
            Manager currentuser=managerService.login(manager);
            if(currentuser!=null) {
                session.setAttribute("currentuser",currentuser);
            }
            return "/manager/manager";
        }
        else if(role.equals("trainer")){
            Coach coach=new Coach();
            coach.setPassword(password);
            coach.setUsername(username);

            Coach currentuser=coachService.login(coach);
            if(currentuser!=null) {
                session.setAttribute("currentuser",currentuser);
            }
            return "/trainer/trainer";
        }
      return "login";
    }

}
